package com.xoa.global.intercptor;

import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by pfl on 2017/12/8.
 */
public class SessionInterceptor implements HandlerInterceptor {

    private String loginUrl;

    private String welcomUrl;

    private String redirectUrl;

    private String mainUrl;

    public String getLoginUrl() {
        return loginUrl;
    }

    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

    public String getWelcomUrl() {
        return welcomUrl;
    }

    public void setWelcomUrl(String welcomUrl) {
        this.welcomUrl = welcomUrl;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    public String getMainUrl() {
        return mainUrl;
    }

    public void setMainUrl(String mainUrl) {
        this.mainUrl = mainUrl;
    }

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String visitUrl = httpServletRequest.getRequestURI();
        Cookie redisSessionId = CookiesUtil.getCookieByName(httpServletRequest, "redisSessionId");
        if (visitUrl.equals(loginUrl)) {
            ContextHolder.setConsumerType("");
            httpServletRequest.getSession().invalidate();//让SESSION失效
            SessionUtils.cleanUserSession(httpServletRequest.getSession(),redisSessionId);
            return true;
        }
        Users user = null;
        String testIds = httpServletRequest.getParameter("JSESSIONID1");
        if (!StringUtils.checkNull(testIds)) {
            HttpSession session = null;
            CommonSessionContext myContext = CommonSessionContext.getInstance();
            session = myContext.getSession(testIds);
            if (session != null) {
                HttpSession sessionNow = httpServletRequest.getSession();
                ContextHolder.setConsumerType("xoa" + (String) session.getAttribute(
                        "loginDateSouse"));
                user = SessionUtils.getSessionInfo(session, Users.class, new Users(),redisSessionId);
                SessionUtils.putSession(sessionNow, user,redisSessionId);
                SessionUtils.putSession(sessionNow, "loginDateSouse", (String) session.getAttribute(
                        "loginDateSouse"),redisSessionId);
                SessionUtils.putSession(sessionNow, "InterfaceModel", (String) session.getAttribute(
                        "InterfaceModel"),redisSessionId);
            }
        }
        if (user == null || user.getUid() == null) {
        } else {
            return true;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        String ids=  httpServletRequest.getParameter("JSESSIONID1");
        httpServletRequest.setAttribute("JSESSIONID1", ids);
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
